using Boo.Lang.Runtime;
using System;
using UnityEngine;

public static class Drawing_tc1
{
	[Serializable]
	public class clip_class
	{
		public float u1;

		public float u2;

		public clip_class(float start, float end)
		{
			u1 = start;
			u2 = end;
		}
	}

	[Serializable]
	public class point_class
	{
		public Vector2 p1;

		public Vector2 p2;

		public point_class(Vector2 start, Vector2 end)
		{
			p1 = start;
			p2 = end;
		}
	}

	[NonSerialized]
	public static Texture2D aaLineTex;

	[NonSerialized]
	public static Texture2D lineTex;

	[NonSerialized]
	public static bool clippingEnabled;

	[NonSerialized]
	public static Rect clippingBounds;

	[NonSerialized]
	public static Material lineMaterial;

	static Drawing_tc1()
	{
		_0024static_initializer_0024();
		aaLineTex = new Texture2D(1, 3, TextureFormat.ARGB32, mipChain: true);
		aaLineTex.SetPixel(0, 0, new Color(1f, 1f, 1f, 0f));
		aaLineTex.SetPixel(0, 1, Color.white);
		aaLineTex.SetPixel(0, 2, new Color(1f, 1f, 1f, 0f));
		aaLineTex.Apply();
		lineTex = new Texture2D(1, 1, TextureFormat.ARGB32, mipChain: true);
		lineTex.SetPixel(0, 1, Color.white);
		lineTex.Apply();
	}

	public static void DrawLineMac(Vector2 pointA, Vector2 pointB, Color color, float width, bool antiAlias)
	{
		Color color2 = GUI.color;
		Matrix4x4 matrix = GUI.matrix;
		float num = width;
		if (antiAlias)
		{
			width *= 3f;
		}
		float num2 = Vector3.Angle(pointB - pointA, Vector2.right) * (float)((!(pointA.y > pointB.y)) ? 1 : (-1));
		float magnitude = (pointB - pointA).magnitude;
		if (!(magnitude <= 0.01f))
		{
			Vector3 vector = new Vector3(pointA.x, pointA.y, 0f);
			Vector3 b = new Vector3((pointB.x - pointA.x) * 0.5f, (pointB.y - pointA.y) * 0.5f, 0f);
			Vector3 zero = Vector3.zero;
			zero = ((!antiAlias) ? new Vector3((0f - num) * 0.5f * Mathf.Sin(num2 * ((float)Math.PI / 180f)), num * 0.5f * Mathf.Cos(num2 * ((float)Math.PI / 180f))) : new Vector3((0f - num) * 1.5f * Mathf.Sin(num2 * ((float)Math.PI / 180f)), num * 1.5f * Mathf.Cos(num2 * ((float)Math.PI / 180f))));
			GUI.color = color;
			GUI.matrix = translationMatrix(vector) * GUI.matrix;
			GUIUtility.ScaleAroundPivot(new Vector2(magnitude, width), new Vector2(-0.5f, 0f));
			GUI.matrix = translationMatrix(-vector) * GUI.matrix;
			GUIUtility.RotateAroundPivot(num2, Vector2.zero);
			GUI.matrix = translationMatrix(vector - zero - b) * GUI.matrix;
			if (antiAlias)
			{
				GUI.DrawTexture(new Rect(0f, 0f, 1f, 1f), aaLineTex);
			}
			else
			{
				GUI.DrawTexture(new Rect(0f, 0f, 1f, 1f), lineTex);
			}
		}
		GUI.matrix = matrix;
		GUI.color = color2;
	}

	public static void DrawLineWindows(Vector2 pointA, Vector2 pointB, Color color, float width, bool antiAlias)
	{
		Color color2 = GUI.color;
		Matrix4x4 matrix = GUI.matrix;
		if (antiAlias)
		{
			width *= 3f;
		}
		float num = Vector3.Angle(pointB - pointA, Vector2.right) * (float)((!(pointA.y > pointB.y)) ? 1 : (-1));
		float magnitude = (pointB - pointA).magnitude;
		Vector3 vector = new Vector3(pointA.x, pointA.y, 0f);
		GUI.color = color;
		GUI.matrix = translationMatrix(vector) * GUI.matrix;
		GUIUtility.ScaleAroundPivot(new Vector2(magnitude, width), new Vector2(-0.5f, 0f));
		GUI.matrix = translationMatrix(-vector) * GUI.matrix;
		GUIUtility.RotateAroundPivot(num, new Vector2(0f, 0f));
		GUI.matrix = translationMatrix(vector + new Vector3(width / 2f, (0f - magnitude) / 2f) * Mathf.Sin(num * ((float)Math.PI / 180f))) * GUI.matrix;
		if (!antiAlias)
		{
			GUI.DrawTexture(new Rect(0f, 0f, 1f, 1f), lineTex);
		}
		else
		{
			GUI.DrawTexture(new Rect(0f, 0f, 1f, 1f), aaLineTex);
		}
		GUI.matrix = matrix;
		GUI.color = color2;
	}

	public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color, float width, bool antiAlias, Rect screen)
	{
		clippingBounds = screen;
		DrawLine(pointA, pointB, color, width);
	}

	public static void curveOutIn(Rect wr, Rect wr2, Color color, Color shadow, int width, Rect screen)
	{
		BezierLine(new Vector2(wr.x + wr.width, wr.y + (float)width + wr.height / 2f), new Vector2(wr.x + wr.width + Mathf.Abs(wr2.x - (wr.x + wr.width)) / 2f, wr.y + (float)width + wr.height / 2f), new Vector2(wr2.x, wr2.y + (float)width + wr2.height / 2f), new Vector2(wr2.x - Mathf.Abs(wr2.x - (wr.x + wr.width)) / 2f, wr2.y + (float)width + wr2.height / 2f), shadow, width, antiAlias: true, 20, screen);
		BezierLine(new Vector2(wr.x + wr.width, wr.y + wr.height / 2f), new Vector2(wr.x + wr.width + Mathf.Abs(wr2.x - (wr.x + wr.width)) / 2f, wr.y + wr.height / 2f), new Vector2(wr2.x, wr2.y + wr2.height / 2f), new Vector2(wr2.x - Mathf.Abs(wr2.x - (wr.x + wr.width)) / 2f, wr2.y + wr2.height / 2f), color, width, antiAlias: true, 20, screen);
	}

	public static void BezierLine(Vector2 start, Vector2 startTangent, Vector2 end, Vector2 endTangent, Color color, float width, bool antiAlias, int segments, Rect screen)
	{
		Vector2 pointA = cubeBezier(start, startTangent, end, endTangent, 0f);
		for (int i = 1; i <= segments; i++)
		{
			Vector2 vector = cubeBezier(start, startTangent, end, endTangent, i / segments);
			DrawLine(pointA, vector, color, width, antiAlias, screen);
			pointA = vector;
		}
	}

	public static Vector2 cubeBezier(Vector2 s, Vector2 st, Vector2 e, Vector2 et, float t)
	{
		float d = 1f - t;
		return s * d * d * d + 3f * st * d * d * t + 3f * et * d * t * t + e * t * t * t;
	}

	public static Matrix4x4 translationMatrix(Vector3 v)
	{
		return Matrix4x4.TRS(v, Quaternion.identity, Vector3.one);
	}

	public static bool clip_test(float p, float q, clip_class u)
	{
		float num = default(float);
		bool result = true;
		if (!(p >= 0f))
		{
			num = q / p;
			if (!(num <= u.u2))
			{
				result = false;
			}
			else if (!(num <= u.u1))
			{
				u.u1 = num;
			}
		}
		else if (!(p <= 0f))
		{
			num = q / p;
			if (!(num >= u.u1))
			{
				result = false;
			}
			else if (!(num >= u.u2))
			{
				u.u2 = num;
			}
		}
		else if (!(q >= 0f))
		{
			result = false;
		}
		return result;
	}

	public static bool segment_rect_intersection(Rect bounds, point_class p)
	{
		float num = p.p2.x - p.p1.x;
		float num2 = default(float);
		clip_class clip_class = new clip_class(0f, 1f);
		int result;
		if (clip_test(0f - num, p.p1.x - bounds.xMin, clip_class) && clip_test(num, bounds.xMax - p.p1.x, clip_class))
		{
			num2 = p.p2.y - p.p1.y;
			if (clip_test(0f - num2, p.p1.y - bounds.yMin, clip_class) && clip_test(num2, bounds.yMax - p.p1.y, clip_class))
			{
				if (!(clip_class.u2 >= 1f))
				{
					p.p2.x = p.p1.x + clip_class.u2 * num;
					p.p2.y = p.p1.y + clip_class.u2 * num2;
				}
				if (!(clip_class.u1 <= 0f))
				{
					p.p1.x = p.p1.x + clip_class.u1 * num;
					p.p1.y = p.p1.y + clip_class.u1 * num2;
				}
				result = 1;
				goto IL_0173;
			}
		}
		result = 0;
		goto IL_0173;
		IL_0173:
		return (byte)result != 0;
	}

	public static void BeginGroup(Rect position)
	{
		clippingEnabled = true;
		clippingBounds = new Rect(0f, 0f, position.width, position.height);
		GUI.BeginGroup(position);
	}

	public static void EndGroup()
	{
		GUI.EndGroup();
		clippingBounds = new Rect(0f, 0f, Screen.width, Screen.height);
		clippingEnabled = false;
	}

	public static void DrawLine(Vector2 start, Vector2 end, Color color, float width)
	{
		if (!RuntimeServices.EqualityOperator(Event.current, null) && Event.current.type == EventType.Repaint)
		{
			point_class point_class = new point_class(start, end);
			lineMaterial.SetPass(0);
			Vector3 vector = default(Vector3);
			Vector3 vector2 = default(Vector3);
			if (width == 1f)
			{
				GL.Begin(1);
				GL.Color(color);
				vector = new Vector3(start.x, start.y, 0f);
				vector2 = new Vector3(end.x, end.y, 0f);
				GL.Vertex(vector);
				GL.Vertex(vector2);
			}
			else
			{
				GL.Begin(7);
				GL.Color(color);
				vector = new Vector3(end.y, start.x, 0f);
				vector2 = new Vector3(start.y, end.x, 0f);
				Vector3 b = (vector - vector2).normalized * width / 2f;
				Vector3 a = new Vector3(start.x, start.y, 0f);
				Vector3 a2 = new Vector3(end.x, end.y, 0f);
				GL.Vertex(a - b);
				GL.Vertex(a + b);
				GL.Vertex(a2 + b);
				GL.Vertex(a2 - b);
			}
			GL.End();
		}
	}

	public static void DrawBox(Rect box, Color color, float width)
	{
		Vector2 vector = new Vector2(box.xMin, box.yMin);
		Vector2 vector2 = new Vector2(box.xMax, box.yMin);
		Vector2 vector3 = new Vector2(box.xMax, box.yMax);
		Vector2 vector4 = new Vector2(box.xMin, box.yMax);
		DrawLine(vector, vector2, color, width);
		DrawLine(vector2, vector3, color, width);
		DrawLine(vector3, vector4, color, width);
		DrawLine(vector4, vector, color, width);
	}

	public static void DrawBox(Vector2 topLeftCorner, Vector2 bottomRightCorner, Color color, float width)
	{
		Rect box = new Rect(topLeftCorner.x, topLeftCorner.y, bottomRightCorner.x - topLeftCorner.x, bottomRightCorner.y - topLeftCorner.y);
		DrawBox(box, color, width);
	}

	public static void DrawRoundedBox(Rect box, float radius, Color color, float width)
	{
		Vector2 vector = default(Vector2);
		Vector2 vector2 = default(Vector2);
		Vector2 vector3 = default(Vector2);
		Vector2 vector4 = default(Vector2);
		Vector2 vector5 = default(Vector2);
		Vector2 vector6 = default(Vector2);
		Vector2 vector7 = default(Vector2);
		Vector2 vector8 = default(Vector2);
		vector = new Vector2(box.xMin + radius, box.yMin);
		vector2 = new Vector2(box.xMax - radius, box.yMin);
		vector3 = new Vector2(box.xMax, box.yMin + radius);
		vector4 = new Vector2(box.xMax, box.yMax - radius);
		vector5 = new Vector2(box.xMax - radius, box.yMax);
		vector6 = new Vector2(box.xMin + radius, box.yMax);
		vector7 = new Vector2(box.xMin, box.yMax - radius);
		vector8 = new Vector2(box.xMin, box.yMin + radius);
		DrawLine(vector, vector2, color, width);
		DrawLine(vector3, vector4, color, width);
		DrawLine(vector5, vector6, color, width);
		DrawLine(vector7, vector8, color, width);
		Vector2 vector9 = default(Vector2);
		Vector2 vector10 = default(Vector2);
		float num = radius / 2f;
		vector9 = new Vector2(vector8.x, vector8.y + num);
		vector10 = new Vector2(vector.x - num, vector.y);
		DrawBezier(vector8, vector9, vector, vector10, color, width);
		vector9 = new Vector2(vector2.x + num, vector2.y);
		vector10 = new Vector2(vector3.x, vector3.y - num);
		DrawBezier(vector2, vector9, vector3, vector10, color, width);
		vector9 = new Vector2(vector4.x, vector4.y + num);
		vector10 = new Vector2(vector5.x + num, vector5.y);
		DrawBezier(vector4, vector9, vector5, vector10, color, width);
		vector9 = new Vector2(vector6.x - num, vector6.y);
		vector10 = new Vector2(vector7.x, vector7.y + num);
		DrawBezier(vector6, vector9, vector7, vector10, color, width);
	}

	public static void DrawConnectingCurve(Vector2 start, Vector2 end, Color color, float width)
	{
		Vector2 a = start - end;
		Vector2 startTangent = start;
		float x = startTangent.x;
		Vector2 vector = a / 2f;
		startTangent.x = x - vector.x;
		Vector2 endTangent = end;
		float x2 = endTangent.x;
		Vector2 vector2 = a / 2f;
		endTangent.x = x2 + vector2.x;
		int segments = Mathf.FloorToInt(a.magnitude / 20f * 3f);
		DrawBezier(start, startTangent, end, endTangent, color, width, segments);
	}

	public static void DrawBezier(Vector2 start, Vector2 startTangent, Vector2 end, Vector2 endTangent, Color color, float width)
	{
		int segments = Mathf.FloorToInt((start - end).magnitude / 20f) * 3;
		DrawBezier(start, startTangent, end, endTangent, color, width, segments);
	}

	public static void DrawBezier(Vector2 start, Vector2 startTangent, Vector2 end, Vector2 endTangent, Color color, float width, int segments)
	{
		Vector2 start2 = CubeBezier(start, startTangent, end, endTangent, 0f);
		for (int i = 1; i <= segments; i++)
		{
			Vector2 vector = CubeBezier(start, startTangent, end, endTangent, i / segments);
			DrawLine(start2, vector, color, width);
			start2 = vector;
		}
	}

	public static Vector2 CubeBezier(Vector2 s, Vector2 st, Vector2 e, Vector2 et, float t)
	{
		float num = 1f - t;
		float num2 = num * t;
		return num * num * num * s + 3f * num * num2 * st + 3f * num2 * t * et + t * t * t * e;
	}

	private static void _0024static_initializer_0024()
	{
		clippingEnabled = true;
	}
}
